web/src/pages/search/[...searchTerm].astro

207 lines · 4598 bytes

  1---
  2import Fuse from 'fuse.js';
  3
  4import Layout from '@layouts/Layout.astro';
  5import { fetchData, slugify } from '@utils/fetch-data';
  6import { prepareSearchItems, searchOptions } from '@utils/do-searchy-searchy';
  7import type { SearchItem } from '@utils/do-searchy-searchy';
  8import Search from '@components/things/Search.svelte';
  9import SmartSuggestions from '@components/things/SmartSuggestions.svelte';
 10import FontAwesome from '@components/form/FontAwesome.svelte';
 11
 12export const prerender = false;
 13
 14let fuse: Fuse<SearchItem>;
 15
 16const categories = (await fetchData())?.categories;
 17
 18const items = prepareSearchItems(categories);
 19
 20fuse = new Fuse(items, searchOptions);
 21
 22const searchTerm = Astro.params.searchTerm;
 23
 24const searchResults = fuse
 25	.search(searchTerm || '')
 26	.map((result) => result.item);
 27
 28const services = searchResults.filter((result) => result.type === 'Service');
 29
 30interface GroupedSection {
 31	sectionName: string;
 32	items: SearchItem[];
 33}
 34
 35interface GroupedCategory {
 36	categoryName: string;
 37	sections: Record<string, GroupedSection>;
 38}
 39
 40const putResultsIntoGroups = () => {
 41	const grouped: Record<string, GroupedCategory> = {};
 42
 43	for (const item of services) {
 44		const categoryName = item.category;
 45		const sectionName = item.sectionName || '';
 46
 47		if (!grouped[categoryName]) {
 48			grouped[categoryName] = { categoryName, sections: {} };
 49		}
 50
 51		if (!grouped[categoryName].sections[sectionName]) {
 52			grouped[categoryName].sections[sectionName] = { sectionName, items: [] };
 53		}
 54
 55		grouped[categoryName].sections[sectionName].items.push(item);
 56	}
 57
 58	return Object.values(grouped).map((category) => ({
 59		categoryName: category.categoryName,
 60		sections: Object.values(category.sections),
 61	}));
 62};
 63
 64const beer = putResultsIntoGroups();
 65---
 66
 67<Layout title="Search | Awesome Privacy">
 68	<section>
 69		<h1>Search</h1>
 70		<Search client:visible data={categories} previousSearch={searchTerm} />
 71	</section>
 72	<SmartSuggestions
 73		client:visible
 74		searchTerm={searchTerm || ''}
 75		categories={categories}
 76	/>
 77	<section class="result-count">
 78		<h3>Deep Search</h3>
 79		<p>
 80			Showing {services.length} results for "{searchTerm}" sorted by relevence
 81		</p>
 82	</section>
 83	<div class="results">
 84		{
 85			beer.map((category) => (
 86				<div class="category">
 87					<a class="category-title" href={`/${slugify(category.categoryName)}`}>
 88						<h3>{category.categoryName}</h3>
 89					</a>
 90					<span class="section-icon">
 91						<FontAwesome iconName={slugify(category.categoryName)} />
 92					</span>
 93					<ul class="section-list">
 94						{category.sections.map((section) => (
 95							<li class="section-item">
 96								<h4>{section.sectionName}</h4>
 97								<ul class="service-list">
 98									{section.items.map((item) => (
 99										<li>
100											<a href={item.url}>{item.name}</a>
101										</li>
102									))}
103								</ul>
104							</li>
105						))}
106					</ul>
107				</div>
108			))
109		}
110	</div>
111</Layout>
112
113<style lang="scss">
114	section {
115		display: flex;
116		flex-direction: column;
117		padding: 1rem;
118		margin: 2rem auto;
119		padding: 1rem;
120		width: calc(100% - 2rem);
121		max-width: 1100px;
122
123		h1 {
124			font-size: 3rem;
125			color: var(--accent-3);
126			font-family: 'Lekton', sans-serif;
127			text-align: center;
128			margin: 1rem 0;
129		}
130	}
131
132	.result-count {
133		padding: 0;
134		width: 80vw;
135		max-width: 900px;
136		margin: 1rem auto 0 auto;
137		h3 {
138			margin: 0;
139			color: var(--accent-3);
140			font-size: 1.6rem;
141		}
142		p {
143			margin: 0;
144			opacity: 0.6;
145		}
146	}
147
148	.results {
149		margin: 0 auto 2rem auto;
150		padding: 1rem;
151		width: calc(100% - 2rem);
152		max-width: 1100px;
153		columns: 6 300px;
154		column-gap: 1rem;
155		.category {
156			padding: 1rem;
157			box-shadow: 4px 4px 0 var(--box-outline);
158			background: var(--accent-fg);
159			border: 2px solid var(--box-outline);
160			border-radius: var(--curve-sm);
161			display: inline-flex;
162			flex-direction: column;
163			width: 85%;
164			margin: 1rem;
165			position: relative;
166			.category-title {
167				text-decoration: none;
168				color: var(--foreground);
169				z-index: 2;
170				position: relative;
171				h3 {
172					font-family: 'Lekton', sans-serif;
173					font-weight: bold;
174					margin: 0;
175					font-size: 1.8rem;
176				}
177			}
178			.section-icon {
179				position: absolute;
180				right: 0.5rem;
181				top: 0.5rem;
182				width: fit-content;
183				:global(svg) {
184					width: 2rem;
185					height: 2rem;
186					opacity: 0.5;
187					text-shadow: 3px 3px 0 black;
188					color: var(--accent-3);
189					transition: all 0.2s ease-in-out;
190				}
191			}
192			.section-list {
193				list-style: none;
194				padding: 0;
195				margin: 0;
196				h4 {
197					margin: 1rem 0 0 0;
198				}
199				.service-list {
200					list-style: none;
201					padding: 0;
202					margin: 0;
203				}
204			}
205		}
206	}
207</style>